<?php
if (! defined ( 'X_APP' )) {
	die ();
}
/**
 *
 * @author lfYang
 *        
 */
class SmartyViewHelper implements IView {
	/**
	 * <h2>渲染引擎实例对象</h2>
	 *
	 * @var Smarty
	 */
	protected $_engine;
	
	/**
	 * __construct
	 * <h2>构造函数</h2>
	 * <p>初始化smarty实例</p>
	 */
	public function __construct() {
		$this->_engine = new Smarty ();
		$tplDir = array (
				// ROOT_PATH . 'templates/metro',
				ROOT_PATH . 'templates/default' 
		);
		$this->_engine->setTemplateDir ( $tplDir );
		$this->_engine->setCompileDir ( ROOT_PATH . 'cache/template_c' );
		$this->_engine->setCacheDir ( ROOT_PATH . 'cache/tpl_cache' );
		$this->_engine->setConfigDir ( ROOT_PATH . 'config' );
		$this->_engine->registerFilter ( 'pre', array (
				$this,
				'remove_customize_delimiter' 
		) );
	}
	
	/**
	 * <h2>返回模板引擎实例</h2>
	 *
	 * @return Smarty
	 */
	public function getEngine() {
		return $this->_engine;
	}
	
	/**
	 * <h2>setSearchPath</h2>
	 * <p>设置模板文件的搜索路径</p>
	 *
	 * @param mixed $path
	 *        	模板文件的搜索路径
	 *        	
	 */
	public function setTemplateDir($path) {
		$this->_engine->setTemplateDir ( $path );
	}
	
	/**
	 * <h2>追加一个模板目录</h2>
	 * <p>追加的模板目录排在搜索顺序的最后</p>
	 *
	 * @param mixed $dir        	
	 */
	public function appendTemplateDir($dir) {
		$oldDir = $this->_engine->getTemplateDir ();
		$this->_engine->setTemplateDir ( array_merge ( ( array ) $oldDir, ( array ) $dir ) ); // php5
			                                                                                      // required
	}
	
	/**
	 * <h2>增加一个优先搜索的模板目录</h2>
	 *
	 * @param mixed $dir        	
	 */
	public function prependTemplateDir($dir) {
		$oldDir = $this->_engine->getTemplateDir ();
		$this->_engine->setTemplateDir ( array_merge ( ( array ) $dir, ( array ) $oldDir ) );
	}
	
	/**
	 * <h2>给模板变量复制</h2>
	 *
	 * @param mixed $key
	 *        	模板变量标签
	 * @param mixed $val
	 *        	模板变量的值
	 * @return void
	 */
	public function __set($key, $val) {
		$this->_engine->assign ( $key, $val );
	}
	
	/**
	 * <h2>判断是否设置了指定的模板变量</h2>
	 *
	 * @param string $key        	
	 * @return boolean
	 */
	public function __isset($key) {
		return $this->_engine->getTemplateVars ( $key ) !== null;
	}
	
	/**
	 * <h2>获取指定模板标签的值</h2>
	 *
	 * @param string $key
	 *        	模板标签名称
	 * @return mixed
	 */
	public function __get($key) {
		$this->_engine->getTemplateVars ( $key );
	}
	
	/**
	 * <h2>删除模板赋值</h2>
	 *
	 * @param string|array $key        	
	 */
	public function __unset($key) {
		$this->_engine->clearAssign ( $key );
	}
	
	/**
	 * <h2>给模板变量赋值</h2>
	 *
	 * @param mixed $spec        	
	 * @param mixed $val        	
	 */
	public function assign($spec, $val = null) {
		if (is_array ( $spec )) {
			$this->_engine->assign ( $spec );
			return;
		}
		$this->_engine->assign ( $spec, $val );
	}
	
	/**
	 *
	 * @param string $spec        	
	 * @param object $obj        	
	 */
	public function registerObject($spec, $obj) {
		if (is_object ( $obj )) {
			$this->_engine->registerObject ( $spec, $obj );
		} else {
			$obj = new stdClass ();
			$this->_engine->registerObject ( $spec, $obj );
		}
	}
	/**
	 * <h2>删除所有的赋值</h2>
	 */
	public function clearAllVars() {
		$this->_engine->clearAllAssign ();
	}
	
	/**
	 *
	 * @see IView::render()
	 */
	public function render($tpl = null) {
		$this->_engine->assign ( 'process_time', microtime ( true ) - $GLOBALS ['start_time'] );
		$this->_engine->display ( $tpl );
	}
	
	/**
	 * <h2>移除自定义模板定界符</h2>
	 *
	 * @param string $source        	
	 * @param Smarty_Internal_Template $template        	
	 * @return string
	 */
	function remove_customize_delimiter($source, Smarty_Internal_Template $template) {
		return str_replace ( array (
				'<!--' . $template->left_delimiter,
				$template->right_delimiter . '-->' 
		), array (
				$template->left_delimiter,
				$template->right_delimiter 
		), $source );
	}
}